package com.sxt.async;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

@Component
public class AsyncTask {
	
	@Async
	public Future<String> task1() throws InterruptedException {
		long startTimeMillis = System.currentTimeMillis();

		Thread.sleep(1000);

		long endTimeMillis = System.currentTimeMillis();

		System.out.println("task1任务耗时："+(endTimeMillis - startTimeMillis)+"ms");
		
		AsyncResult<String> result = new AsyncResult<String>("task1执行完毕！");
		
		return result;
	}
	
	@Async
	public Future<String> task2() throws InterruptedException {
		long startTimeMillis = System.currentTimeMillis();

		Thread.sleep(2000);

		long endTimeMillis = System.currentTimeMillis();

		System.out.println("task2任务耗时："+(endTimeMillis - startTimeMillis)+"ms");
		
		AsyncResult<String> result = new AsyncResult<String>("task2执行完毕！");
		
		return result;
	}
	
	@Async
	public Future<String> task3() throws InterruptedException {
		long startTimeMillis = System.currentTimeMillis();

		Thread.sleep(3000);

		long endTimeMillis = System.currentTimeMillis();

		System.out.println("task3任务耗时："+(endTimeMillis - startTimeMillis)+"ms");
		
		AsyncResult<String> result = new AsyncResult<String>("task3执行完毕！");
		
		return result;
	}
	
	@Async
	public Future<String> task4() throws InterruptedException {
		long startTimeMillis = System.currentTimeMillis();

		Thread.sleep(3000);

		long endTimeMillis = System.currentTimeMillis();

		System.out.println("task3任务耗时："+(endTimeMillis - startTimeMillis)+"ms");
		
		AsyncResult<String> result = new AsyncResult<String>("task3执行完毕！");
		
		return result;
	}
}
